package com.example.opengl.KText2D;

import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;

public class KSeekText2D {
//    private FloatBuffer mFVertexBuffer;
//    private ByteBuffer  mColorBuffer;
//    private ByteBuffer  mIndexBuffer;
//    private FloatBuffer mfTexBuffer;
//
////    private int mTextureID;
//    private int[] textures = new int[1];
//
//    public KSeekText2D(){
////        float vertices[] = {
////                -1.0f, -1.0f,
////                 1.0f, -1.0f,
////                -1.0f,  1.0f,
////                 1.0f,  1.0f
////        };
//
////        float vertices[] = {
////                -0.0f, -0.5f,
////                 0.5f, -0.5f,
////                -0.0f,  0.0f,
////                 0.5f,  0.0f
////        };
//
//        float vertices[] = {
//              -1.0f,  1.0f,
//               0.0f,  1.0f,
//              -1.0f, -0.0f,
//               0.0f, -0.0f
//        };
//
//        float texture[] = {
//               -1.0f,  1.0f,
//                0.0f,  1.0f,
//               -1.0f, -0.0f,
//                0.0f, -0.0f
//        };
//
//        byte indices[] = {
//                2, 1, 3,
//                2, 0, 1
//        };
//
//        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
//        vbb.order(ByteOrder.nativeOrder());
//        mFVertexBuffer = vbb.asFloatBuffer();
//        mFVertexBuffer.put(vertices);
//        mFVertexBuffer.position(0);
//
//        vbb = ByteBuffer.allocateDirect(texture.length * 4);
//        vbb.order(ByteOrder.nativeOrder());
//        mfTexBuffer = vbb.asFloatBuffer();
//        mfTexBuffer.put(texture);
//        mfTexBuffer.position(0);
//
//        mIndexBuffer = ByteBuffer.allocate(indices.length);
//        mIndexBuffer.put(indices);
//        mIndexBuffer.position(0);
//    }
//
//    public void TexCreate(GL10 gl)
//    {
////        int[] textures = new int[1];
////        gl.glGenTextures(1, textures, 0);
////        mTextureID = textures[0];
////        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);
//        Bitmap mBitmap;
//        Canvas mCanvas;
//
//        Bitmap.Config config =   Bitmap.Config.ARGB_8888;
//        mBitmap = Bitmap.createBitmap(128, 128, config);
//        mCanvas = new Canvas(mBitmap);
//        mBitmap.eraseColor(0);
//        mCanvas.drawColor(0x00ffffff);
//
//        Paint Pnt = new Paint();
//        Pnt.setColor(0xff00ff00);
//        Pnt.setTextSize(128);
//        Pnt.setAntiAlias(false);
//        Pnt.setTextScaleX(1);
//
//        mCanvas.drawText("B", 0, 128, Pnt);
//
//        gl.glGenTextures(1, textures, 0);
//        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
//
//        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
//        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
//        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
//        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
//        gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);
//
//        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, mBitmap, 0);
//
////        FileOutputStream out = null;
////        try {
////            out = new FileOutputStream("/sdcard/Output/captureScreen.png");
////            if ( mBitmap.compress(CompressFormat.PNG, 100, out) );
////        } catch (Exception e) {
////            e.printStackTrace();
////        } finally {
////             try {
////                 out.flush();
////                 out.close();
////             } catch (IOException e) {
////                 e.printStackTrace();
////             }
////        }
//
//        mBitmap.recycle();
//    }
//
//    public void draw(GL10 gl) {
////        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);
//
//
//        gl.glMatrixMode(GL10.GL_MODELVIEW);
//        gl.glLoadIdentity();
//        gl.glPushMatrix();
////        gl.glTranslatef(0f, 0.25f, 0f);
//
//        /* Start Draw */
//        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
//
//        gl.glFrontFace(GL11.GL_CW);
//
//        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
//        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//
//        gl.glVertexPointer(2, GL11.GL_FLOAT, 0, mFVertexBuffer);
//        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mfTexBuffer);
//
//        gl.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_BYTE, mIndexBuffer);
//        gl.glFrontFace(GL11.GL_CCW);
//
//        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
//        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//        /* End Draw */
//
//        gl.glPopMatrix();
//        gl.glLoadIdentity();
//    }

    private FloatBuffer vertexBuffer;
    private FloatBuffer textureBuffer;
    private ByteBuffer indexBuffer;

    private int[] textures = new int[1];

    private float vertices[] = {
            0.0f, 0.0f, 0.0f,
            1.0f, 0.0f, 0.0f,
            1.0f, 1.0f, 0.0f,
            0.0f, 1.0f, 0.0f
    };

    private float texture[] = {
            0.0f, 0.0f,
            1.0f, 0.0f,
            1.0f, 1.0f,
            0.0f, 1.0f
    };

    private byte indices[] = {
            0, 1, 2,
            0, 2, 3
    };

    public KSeekText2D() {
        ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
        byteBuf.order(ByteOrder.nativeOrder());
        vertexBuffer = byteBuf.asFloatBuffer();
        vertexBuffer.put(vertices);
        vertexBuffer.position(0);

        byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
        byteBuf.order(ByteOrder.nativeOrder());
        textureBuffer = byteBuf.asFloatBuffer();
        textureBuffer.put(texture);
        textureBuffer.position(0);

        indexBuffer = ByteBuffer.allocateDirect(indices.length);
        indexBuffer.put(indices);
        indexBuffer.position(0);
    }

    public void draw(GL10 gl) {
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();

        gl.glPushMatrix();
        gl.glScalef(1f, 1f, 1f);
        gl.glTranslatef(0f, 0f, 0f);

        gl.glMatrixMode(GL10.GL_TEXTURE);
        gl.glLoadIdentity();
        gl.glTranslatef(0f, 0f, 0f);

        ////////////////////////////
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        gl.glFrontFace(GL10.GL_CCW);
        gl.glEnable(GL10.GL_CULL_FACE);
        gl.glCullFace(GL10.GL_BACK);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

        gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);

        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glDisable(GL10.GL_CULL_FACE);
/////////////////////////
        gl.glPopMatrix();
        gl.glLoadIdentity();

    }

    public void loadTexture(GL10 gl, int texture, Context context) {
        InputStream imagestream = context.getResources().openRawResource(texture);
        Bitmap bitmap = null;

        try {
            bitmap = BitmapFactory.decodeStream(imagestream);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            try {
                imagestream.close();
                imagestream = null;
            } catch(Exception e) {
                e.printStackTrace();
            }
        }

        gl.glGenTextures(1, textures, 0);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

        bitmap.recycle();
    }

}
